package com.leyou.item.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exception.pojo.ExceptionEnum;
import com.leyou.common.exception.pojo.LyException;
import com.leyou.common.pojo.PageResult;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import org.apache.ibatis.annotations.Lang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BrandService extends ServiceImpl<BrandMapper, Brand> {

    @Autowired(required = false)
    private BrandMapper brandMapper;

    /**
     * 获取分类页结果对象
     * @param key  搜索关键字
     * @param page  当前页码
     * @param rows  每页大小
     * @param sortBy 排序字段
     * @param desc
     * @return
     */
    public PageResult<Brand> brandPageQuery(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
        //设置分页查询条件
        IPage<Brand> iPage = new Page<>(page,rows);
        //构建查询条件的对象
        QueryWrapper<Brand> queryWrapper = Wrappers.query();

        //判断是否存在搜索条件
        if (StringUtils.isNotEmpty(key)){  //存在搜索条件
            //设置查询条件
            queryWrapper.eq("id", key)
                    .or()
                    .like("name", key)
                    .or()
                    .eq("letter", key);
        }

        //判断是否有排序条件
        if(StringUtils.isNotEmpty(sortBy)){ //存在查询条件
            //设置排序条件
            if(desc){
                queryWrapper.orderByDesc(sortBy);
            }else {
                queryWrapper.orderByAsc(sortBy);
            }
        }

        //执行查询条件
        iPage = page(iPage,queryWrapper);
        //创建自定义分页对象
        PageResult<Brand> pageResult = new PageResult<Brand>(
                iPage.getTotal(),
                iPage.getPages(),
                iPage.getRecords()
        );
        return pageResult;
    }

    /**
     * 保存新增品牌
     * @param brand
     * @param cids
     */
    public void saveBrand(Brand brand, List<Long> cids) {
        try {
            //保存商品
            save(brand);
            //维护商品--分类中间表
            brandMapper.insertCategoryAndBrand(brand.getId() , cids);
        } catch (Exception e) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }


    /*根据品牌id查询品牌对象*/
    public Brand findBrandById(Long id) {
        //查询brand对象
        Brand brand = getById(id);
        //判断brand是否存在
        if (brand==null){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return brand;
    }


    /*根据分类id查询品牌列表*/
    public List<Brand> findBrandByCid(Long id) {

        List<Brand> brandList = brandMapper.findBrandByCid(id);

        if (CollectionUtils.isEmpty(brandList)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return brandList;
    }


    /*根据品牌id集合查询品牌对象集合*/
    public List<Brand> findBrandsByIds(List<Long> ids) {
        List<Brand> brandList = brandMapper.selectBatchIds(ids);
        if (CollectionUtils.isEmpty(brandList)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return brandList;
    }
}
